package com.zhz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhz.domain.UserBank;
import com.zhz.dto.UserBankDto;
import com.zhz.feign.UserBankServiceFeign;
import com.zhz.mappers.UserBankDtoMapper;
import com.zhz.model.R;
import com.zhz.service.UserBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：zhz
 * @date ：Created in 2021/01/02
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 **/
@RestController
@RequestMapping("/userBanks")
@Api(tags = "会员的银行卡管理")
public class UserBankController implements UserBankServiceFeign {

    @Autowired
    private UserBankService userBankService;

    @GetMapping
    @ApiOperation(value = "分页查询会员信息")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "userId", value = "会员的id")
    })
    @PreAuthorize("hasAuthority('user_bank_query')")
    public R<Page<UserBank>> findByPage(Page<UserBank> page, Long userId) {
        page.addOrder(OrderItem.desc("last_update_time"));
        return R.ok(userBankService.findByPage(page, userId));
    }

    @PostMapping("/status")
    @ApiOperation(value = "修改银行卡状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "银行卡id"),
            @ApiImplicitParam(name = "status", value = "银行卡状态")
    })
    public R updateStatus(Long id, Byte status) {
        UserBank userBank = new UserBank();
        userBank.setId(id);
        userBank.setStatus(status);
        return userBankService.updateById(userBank) == true ? R.ok() : R.fail("银行卡状态修改失败");
    }
    @PatchMapping
    @ApiOperation(value = "修改银行卡")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userBank",value = "银行卡信息")
    })
    public R updateUserBank(@RequestBody @Validated UserBank userBank){
        return userBankService.updateById(userBank) == true ? R.ok() : R.fail("银行卡修改失败");
    }

    @PostMapping("/current")
    @ApiOperation(value = "查询当前用户的银行卡")
    public R<UserBank> getCurrentUserBank(){
        long userId = Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        return R.ok(userBankService.getCurrentUserBank(userId));
    }
    @PostMapping("/bind")
    @ApiOperation(value = "绑定银行卡")
    public  R bindBank(@RequestBody @Validated UserBank userBank){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean isOk = userBankService.bindBank(userId,userBank) ;
        return userBankService.updateById(userBank) == true ? R.ok() : R.fail("绑定失败");
    }
    @Override
   public UserBankDto getUserBankInfo(Long userId) {
        UserBank currentUserBank = userBankService.getCurrentUserBank(userId);
        UserBankDto userBankDto = UserBankDtoMapper.INSTANCE.toConvertDto(currentUserBank);
        return userBankDto ;
    }

}
